/*******************************************************************************
 * 用户列表视图
 * 
 * @class Microblogging.UserList
 * @extends Ext.List
 */
Microblogging.view.user.UserList = Ext.extend(Ext.Panel, {
	layout : 'fit',
	initComponent : function() {
		this.list = new Ext.List({
			grouped : true,
			simpleSelect :true,
			cls:'x-user-list',
			itemTpl : ['<div class="x-user-list-item">', 
							'<div class="x-user-list-item-portrait">',
								'<img class="x-user-list-item-portrait-img" src="{portrait}" />',
								'<img class="x-user-list-item-portrait-type-icon icon-{type}-16" src=""/>',
							'</div>', 
							'<span class="x-user-list-item-name">{nickName}', '</span>',
							'<img src="" class="x-user-list-item-status status-',
								'<tpl if="status == 0">stop</tpl>',
								'<tpl if="status == 1">normal</tpl>',
								'<tpl if="status == 2">only-send</tpl>',
							'-icon"/>',
						'</div>'],
			onItemDisclosure : function(record, item, index) {
				alert(index);
			},
			store : new Ext.data.Store({
						model : App.models.USER_LIST,
						getGroupString : function(record) {
					        return record.get('displayType');
					    },
						proxy : {
							type : 'localstorage',
							id : App.models.USER_LIST
						},
						data:[
							{id:'1',nickName:'code',displayType:'新浪微博',type:'sina',portrait:null,status:1},
							{id:'2',nickName:'icode',displayType:'腾讯微博',type:'tencent',status:2},
							{id:'3',nickName:'汉字效果',displayType:'网易微博',type:'netease',status:0},
							{id:'4',nickName:'汉字效果',displayType:'搜狐微博',type:'sohu',status:2},
							{id:'5',nickName:'acode',displayType:'新浪微博',type:'sina',status:1},
							{id:'2',nickName:'汉字效果',displayType:'腾讯微博',type:'tencent',status:0},
							{id:'3',nickName:'ocode',displayType:'网易微博',type:'netease',status:0},
							{id:'4',nickName:'汉字效果',displayType:'搜狐微博',type:'sohu',status:2},
							{id:'5',nickName:'acode',displayType:'新浪微博',type:'sina',status:1},
							{id:'2',nickName:'汉字效果',displayType:'腾讯微博',type:'tencent',status:0},
							{id:'3',nickName:'ocode',displayType:'网易微博',type:'netease',status:1},
							{id:'4',nickName:'汉字效果',displayType:'搜狐微博',type:'sohu',status:2},
							{id:'5',nickName:'acode',displayType:'新浪微博',type:'sina',status:0},
							{id:'2',nickName:'汉字效果',displayType:'腾讯微博',type:'tencent',status:1},
							{id:'3',nickName:'ocode',displayType:'网易微博',type:'netease',status:2},
							{id:'4',nickName:'汉字效果',displayType:'搜狐微博',type:'sohu',status:0},
							{id:'5',nickName:'acode',displayType:'新浪微博',type:'sina',status:1}
						]
					}),
			listeners:{
				selectionchange:function(sModel,records){
					var bar = this.toolbar,
					delUser = bar.getComponent('delUser'),
					ssUser = bar.getComponent('ssUser');
					if(records.length > 0){
						if(delUser.isDisabled()){
							delUser.enable(true);
						}
						if(ssUser.isDisabled()){
							ssUser.enable(true);
						}
					}else{
						if(!delUser.isDisabled()){
							delUser.disable(true);
						}
						if(!ssUser.isDisabled()){
							ssUser.disable(true);
						}
					}
				},
				scope:this
			}
		});
		this.toolbar = new Ext.Toolbar({
			dock : 'bottom',
			items : [{
				xtype : 'selectbutton',
				// text : '添加',
				iconCls : 'add',
				options : [{
							text : '腾讯微博',
							value : 'tencent',
							icon : 'icon-tencent-24'
						}, {
							text : '新浪微博',
							value : 'sina',
							icon : 'icon-sina-24'
						}, {
							text : '网易微博',
							value : 'netease',
							icon : 'icon-netease-24'
						}, {
							text : '搜狐微博',
							value : 'sohu',
							icon : 'icon-sohu-24'
						}],
				listeners : {
					change : function(me, record) {
						var type = record.get(me.valueField);
						Ext.redirect('user/add/' + type, false);
					},
					scope:this
				}
			}, {
				// text : '删除',
				iconCls : 'trash',
				id:'delUser',
				disabled: true,
				handler : function() {
					Ext.redirect('user/del/' + this.getSelectUserIds(), false);
				},
				scope:this
			}, {
				xtype:'selectbutton',
				// text : '停/启用',
				iconCls : 'arrow_right',
				id:'ssUser',
				listIconCls:'status-icon',
				disabled: true,
				options:[
					{text:'停用',value:'0',icon:'status-stop-icon'},
					{text:'启用',value:'1',icon:'status-normal-icon'},
					{text:'仅发送',value:'2',icon:'status-only-send-icon'}
				],
				listeners:{
					change:function(me,record){
						var status = record.get(me.valueField);
						Ext.redirect('user/status/' + this.getSelectUserIds() +'/'+ status, false);
					},
					scope:this
				}
			}, {
				iconCls : 'more',
				handler : function() {
					if (!this.more) {
						this.more = new Microblogging.MenuSheet({
									defaults : {
										scope : this
									},
									items : [{
												iconCls : 'home',
												text : '首页',
												handler : function() {
													this.more.hide();
													Ext.redirect('home');
												}
											},{
												text : '设置',
												iconCls : 'settings',
												handler : function() {
													Ext.redirect('user/set/' + 'userId');
												}
											},{
												iconCls:'bookmarks',
												text:'帮助',
												handler:function(){
													this.more.hide();
													Ext.Msg.alert('帮助','帮助内容后续补充');
												}
											}, {
												iconCls : 'reply',
												text : '取消',
												handler : function() {
													this.more.hide();
												}
											}]
								});
					}
					this.more.show();
				}
			}]
		});
		Ext.apply(this, {
					dockedItems : [this.toolbar],
					items : [this.list]
				});
		Microblogging.view.user.UserList.superclass.initComponent.apply(this,
				arguments);
	},
	getSelectUserIds:function(id,str,sp){
		var records = this.list.getSelectedRecords(),
		userIds=[];
		for(var i=0;i<records.length;i++){
			userIds.push(records[i].get(id || 'id'));
		}
		if(userIds.length < 1){
			return null;
		}
		return str === false ? userIds : userIds.join(sp || ',');
	},
	destroy : function() {
		Microblogging.view.user.UserList.superclass.destroy.apply(this,
				arguments);
		Ext.destroy(this.toolbar, this.list,this.more);
	}
});
Ext.reg('userlist', Microblogging.view.user.UserList);